﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataAccess;
using BusinessFacade;
using Entities;
using Comm;

namespace MedicareNoteManagerWeb.Web
{
    public partial class Roles : BasePage
    {
        sys_user_Model model1 = new sys_user_Model();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                LoadList();
            }
        }
        public void LoadList()
        {
            DataTable dt = new DataTable();
            string rolename = txtRoleName.Text.Trim();
            dt = Busi_sys_Role.Getlist(rolename);
            Grd_User.DataSource = dt;
            Grd_User.DataBind();
        }
        protected void Grd_User_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Grd_User.EditIndex = e.NewEditIndex;
            LoadList();
        }
        protected void Grd_User_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            Grd_User.EditIndex = -1;
            LoadList();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            LoadList();
        }
        protected void Grd_User_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView theGrid = sender as GridView;
            int newPageIndex = 0;
            if (e.NewPageIndex == -3)
            {
                //点击了Go按钮
                TextBox txtNewPageIndex = null;

                //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                GridViewRow pagerRow = theGrid.BottomPagerRow;

                if (pagerRow != null)
                {
                    //得到text控件
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
                }
                if (txtNewPageIndex != null)
                {
                    //得到索引
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                }
            }
            else
            {
                //点击了其他的按钮
                newPageIndex = e.NewPageIndex;
            }
            //防止新索引溢出
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

            //得到新的值
            theGrid.PageIndex = newPageIndex;

            //重新绑定
            LoadList();
        }

        protected void Grd_User_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int ID = Convert.ToInt32(Grd_User.DataKeys[e.RowIndex].Values[0].ToString());
             if(new Busi_sys_Role().Delete(ID))
            {
                
                MessageBox.ShowDialog(Page, "删除成功！", "success");
                LoadList();
            }
           else
            {
                MessageBox.ShowDialog(Page, "删除失败！", "error");
                LoadList();
            }
        }

        protected void Grd_User_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                model1 = (sys_user_Model)Session["UserModel"];
                sys_role_Model model = new sys_role_Model();
                model.RoleID = Convert.ToInt32(Grd_User.DataKeys[e.RowIndex].Values[0].ToString());
                model.ModifyDate = DateTime.Now;
                model.ModifyUserID = model1.UserID;
                model.RoleName = ((TextBox)Grd_User.Rows[e.RowIndex].FindControl("txtRoleName")).Text.Trim();
                model.Remark = ((TextBox)Grd_User.Rows[e.RowIndex].FindControl("txtRemark")).Text.Trim();
                if (model.RoleName == "")
                {
                    MessageBox.ShowDialog(Page, "请填填写角色名称！", "error");
                    return;
                }
                if (model.Remark == "")
                {
                    MessageBox.ShowDialog(Page, "请填填写角色描述！", "error");
                    return;
                }
                else
                {
                    if (new Busi_sys_Role().Update(model))
                    {
                        MessageBox.ShowDialog(Page, "更新失败！", "success");
                        LoadList();
                    }
                    else
                    {
                        MessageBox.ShowDialog(Page, "修改成功！", "success");
                        LoadList();
                    }
                    
                }
                Grd_User.EditIndex = -1;
                LoadList();
            }
            catch (Exception ex)
            {
                MessageBox.ShowDialog(Page, "修改失败！" + ex.Message, "error");
            }
        }

        protected void BtnFind_Click(object sender, EventArgs e)
        {
            LoadList();
        }


    }
}